//
//  HWReadRecordView.m
//  嗨文
//
//  Created by 响当当 on 2019/2/18.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWCashDrawView.h"
#import "HWCashDrawHeadView.h"
#import "HWReadRecordCell.h"

@interface HWCashDrawView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;//列表

@property (nonatomic, strong)HWCashDrawHeadView *readRecordHeadView;
@property (nonatomic, strong)NSMutableArray *arr;
@property (nonatomic, strong)NSString *segmentNum;
@property (nonatomic, assign)BOOL hasNextPage;
@property (nonatomic, strong)NSString *pageNum;
@end

@implementation HWCashDrawView

- (NSMutableArray *)arr
{
    if (!_arr)
    {
//        _arr = @[@{@"title":@"追风的少年",@"haiFriend":@"1",@"vip":@"1",@"haiLevel":@"1",@"connectMATime":@"扫码时间：2018-12-20 15:23:35",@"joinHaiTime":@"加入嗨赚：2018-12-20 15:23:35",@"money":@"+56328.26",@"friend":@"86个嗨友"},
//                 @{@"title":@"追风的少年",@"haiFriend":@"0",@"vip":@"1",@"haiLevel":@"0",@"connectMATime":@"扫码时间：2018-12-20 15:23:35",@"joinHaiTime":@"加入嗨赚：2018-12-20 15:23:35",@"money":@"+56328.26",@"friend":@"86个嗨友"},
//                 @{@"title":@"追风的少年",@"haiFriend":@"1",@"vip":@"0",@"haiLevel":@"1",@"connectMATime":@"扫码时间：2018-12-20 15:23:35",@"joinHaiTime":@"加入嗨赚：2018-12-20 15:23:35",@"money":@"+56328.26",@"friend":@"86个嗨友"}];
        _arr = [[NSMutableArray alloc]init];
    }
    return _arr;
}

- (HWCashDrawHeadView *)readRecordHeadView
{
    if (!_readRecordHeadView)
    {
        _readRecordHeadView = [[HWCashDrawHeadView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,353*kUIScaleBasePlus)];
        WeakSelf(self)
        _readRecordHeadView.getBackSegmentChangeBlock = ^(NSString *num) {
            [SVProgressHUD showWithStatus:@"加载中"];
            [weakSelf.arr removeAllObjects];
            weakSelf.pageNum = @"1";
            weakSelf.hasNextPage = YES;
            weakSelf.segmentNum = num;
            [weakSelf changeSegment];
        };
    }
    return _readRecordHeadView;
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
        self.tableView.tableHeaderView = self.readRecordHeadView;
        
//        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadUpData)];
//        [header setTitle:@"Loading ..." forState:MJRefreshStateRefreshing];
//        // 设置字体
//        header.stateLabel.font = [UIFont systemFontOfSize:17];
//
//        // 设置颜色
//        header.stateLabel.textColor = LCHexColor(0x1f1f1f);
        
        //下拉刷新
//        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadUpData)];
//        _tableView.mj_header = header;
        
        //上拉刷新
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        //设置上拉刷新属性
        [footer setTitle:@"加载更多" forState:MJRefreshStateIdle];
        [footer setTitle:@"Loading ..." forState:MJRefreshStateRefreshing];
        [footer setTitle:@"去首页看看" forState:MJRefreshStateNoMoreData];
        
        // 设置字体
        footer.stateLabel.font = [UIFont systemFontOfSize:17];
        
        // 设置颜色
        footer.stateLabel.textColor = LCHexColor(0x1f1f1f);
        
        _tableView.mj_footer = footer;
//        _tableView.mj_footer.hidden = YES;
        [_tableView.mj_footer beginRefreshing];
    }
    return _tableView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.segmentNum = @"0";
        self.pageNum = @"1";
        self.hasNextPage = YES;
        [self addSubview:self.tableView];
    }
    return self;
}

#pragma mark - tableView delegate & datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 91*kUIScaleBasePlus;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HWReadRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HWReadRecordCell"];
    if (!cell)
    {
        cell = [[HWReadRecordCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HWReadRecordCell"];
        cell.selectionStyle = 0;
    }
    
    if ([self.arr isValidArray])
    {
        HWCashRecordListModel *model = [[HWCashRecordListModel alloc]initWithDic:self.arr[indexPath.row]];
        [cell getDate:model];
    }
    
    return cell;
}

- (void)getDate:(HWCashDrawModel *)model
{
    NSDictionary *dics = @{@"draw":[NSString stringWithFormat:@"%@\n已提现总奖励",[model.hasWithdrawedCash stringValue]],
                          @"ableDraw":[NSString stringWithFormat:@"%@\n可提现余额",model.canWithdrawCash]
                          };
    [_readRecordHeadView getDate:dics];
}

- (void)getDateSegmentNum:(NSDictionary *)dic
{
    [_readRecordHeadView getDateSegmentNum:dic];
}

- (void)getCashDrawList:(NSArray *)arr hasNextPage:(NSNumber *)hasNextPage;
{
    
    [SVProgressHUD dismiss];
    if ([arr isValidArray])
    {
        if (self.hasNextPage)
        {
            self.tableView.mj_footer.hidden = NO;
            [self.arr addObjectsFromArray:arr];
            self.hasNextPage = [hasNextPage boolValue];
            [self.tableView reloadData];
        }
        else
        {
            self.tableView.mj_footer.hidden = YES;
        }
        
    }
    else
    {
        self.tableView.mj_footer.hidden = YES;
    }
}

- (void)loadNewData
{
    [SVProgressHUD showWithStatus:@"加载中"];
    WeakSelf(self)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        if (weakSelf.getBackInfoBlock)
        {
            
            if (self.hasNextPage)
            {
                NSInteger num = self.pageNum.integerValue + 1;
                self.getBackInfoBlock(self.segmentNum,[NSString stringWithFormat:@"%ld",num]);
                self.pageNum = [NSString stringWithFormat:@"%ld",num];
            }
            else
            {
                self.getBackInfoBlock(@"0",@"1");
            }
        }
        
        [self.tableView.mj_footer endRefreshing];
        
    });
}

//- (void)loadUpData
//{
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
//        [self.tableView.mj_header endRefreshing];
//
//    });
//}

- (void)changeSegment
{
    if (self.changeSegmentBlock)
    {
        self.changeSegmentBlock(self.segmentNum);
    }
}
@end
